package com.dhcc.DRGsHosp.application.impl;

import java.math.BigDecimal;
import java.sql.Clob;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.inject.Named;

import org.dayatang.domain.EntityRepository;
import org.dayatang.domain.InstanceFactory;
import org.dayatang.domain.SqlQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.SpecialMonitorApplication;
import com.dhcc.DRGsHosp.core.domain.IndexCal;
import com.dhcc.DRGsHosp.infra.util.DrgsIndex;
import com.dhcc.DRGsHosp.infra.util.MonitorResultUtil;
import com.dhcc.DRGsHosp.infra.util.StringUtils;

/**
 * @title		:SpecialMonitorApplicationImpl.java
 * @description	:
 * @author		:lileizheng
 * @date		:2016年3月4日
 * @version     :1.0
 * @copyright   :DHCC 2013-2014 
 */
@Named
@Transactional
public class SpecialMonitorApplicationImpl implements SpecialMonitorApplication {
	
	private static final Logger logger = LoggerFactory.getLogger(SpecialMonitorApplicationImpl.class);
	
	@Autowired
	private NamedParameterJdbcTemplate jdbcTempSeverice;
	private EntityRepository getRepository() {
		EntityRepository repository = null;
		if (repository == null) {
			repository = InstanceFactory.getInstance(
					EntityRepository.class, "repository");
		}
		return repository;
	}
	
	@Cacheable(value = "drgscom3.1.1", key = "'specialMonitor' + #m.specCode + "
			+ "#m.dateFrom + #m.dateTo + #m.stdId + #m.verRegion + #m.quotaCode + #m.hospCode")
	@Override
	public List<Object[]> monitor(IndexCal m) {
		String dateFrom = m.getDateFrom();
		String dateTo = m.getDateTo();
		String stdId = m.getStdId();
		String verRegion = m.getVerRegion();
		String quotaCode = m.getQuotaCode();
		String specCode = m.getSpecCode();
		String verCode = m.getVerCode();
		//院区编码
		String hospCode=m.getHospCode(); 
		String sqlStr = " select 1 from dual where 1>2 ";
		Map<String, Object> map = new HashMap<>();
		StringBuilder sql = new StringBuilder();
		sql.append(" select select_sql from DRGs.t_quotaselect where quota_code='"+quotaCode+"' ");
		List<Object> sqlList = getRepository().createSqlQuery(sql.toString()).list();
		if(sqlList.size()>0){
			Clob clob = (Clob) sqlList.get(0);
			try {
				sqlStr = MonitorResultUtil.ClobToString(clob);
				if(MonitorResultUtil.hasLength(specCode)){
					sqlStr = sqlStr.replace(" 1=1 ", " spec_code=:specCode and  1=1  ");
					map.put("specCode", specCode);
				}
				// 6=6 仅用于拼接临床专业条件
				if(MonitorResultUtil.hasLength(specCode)){
					sqlStr = sqlStr.replace(" 6=6 ", " spec_code=:specCode and  6=6  ");
					map.put("specCode", specCode);
				}
				//***医院编码*** 有的指标与与首页无关，例如 spQuaScores ，但页面却有hospCode 的参数
				if(StringUtils.hasLength(hospCode) && sqlStr.contains(" 1=1 ")){
					sqlStr = sqlStr.replaceAll(" 1=1 ", " instr(:hospCode,a.p00010001)>0 and  1=1  ");
					map.put("hospCode", hospCode);
				}
				if(sqlStr.contains(":ver_region")){
					map.put("ver_region", verRegion);
				}
				if(sqlStr.contains(":dateFrom")){
					map.put("dateFrom", dateFrom);
				}
				if(sqlStr.contains(":dateTo")){
					map.put("dateTo", dateTo);
				}
				if(sqlStr.contains(":stdId")){
					map.put("stdId", stdId);
				}
				if(sqlStr.contains(":verCode")){
					map.put("verCode", verCode);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		SqlQuery sqlQuery = getRepository().createSqlQuery(sqlStr);
		if(!map.isEmpty()){
			sqlQuery = sqlQuery.setParameters(map);
		}
		List<Object[]> resultList = new ArrayList<>();
		try {
			resultList = sqlQuery.list();
		} catch (Exception e) {
			e.printStackTrace();
			// 将错误指标代码和sql和错误信息写入文件
			if (sqlStr == null || "".equals(sqlStr) || "null".equals(sqlStr)) {
				sqlStr = "指标SQL不存在";
			}
			logger.error("时间：" + new Date() + "--" + quotaCode + ":\r\n" + sqlStr + "\r\n" + e.getMessage());
		}
		return resultList;
	}
	
	@Cacheable(value = "drgscom3.1.1", key = "'specialGetWeights' + #result.dateFrom + #result.dateTo + "
			+ "#result.stdId + #result.verRegion + #result.specCode + #result.hospCode")
	@Override
	public List<String> getWeights(IndexCal result) {
		String dateFrom = result.getDateFrom();
		String dateTo = result.getDateTo();
		String stdId = result.getStdId();
		String verRegion = result.getVerRegion();
		String specCode = result.getSpecCode();
		//院区编码
		String hospCode=result.getHospCode(); 
		StringBuilder sql = new StringBuilder();
		sql.append(" select e.as_weight from drgs.mr_baseinfo a \n ");
		sql.append(" left join drgs.mr_ipinfo ipinfo on a.pmainindex = ipinfo.pmainindex \n ");
		sql.append(" left join drgs.t_patientdrg b  \n ");
		sql.append(" on ipinfo.pmainindex=b.parid and b.ver_region='"+verRegion+"' \n ");
		sql.append(" left join drgs.t_clispecdrgs cd \n ");
		sql.append(" on cd.csd_drg_code = b.pardrg_code and cd.csd_ver_code = b.ver_code \n ");
		sql.append(" left join drgs.t_annualstd e \n ");
		sql.append(" on b.pardrg_code=e.as_drg_code and e.AS_ID='"+stdId+"' \n ");
		sql.append(" where to_char(ipinfo.P00290000,'yyyy-mm-dd') between  '"+dateFrom+"'  and  '"+dateTo+"' \n ");
		sql.append(" and e.as_weight is not null \n ");
		if(StringUtils.hasLength(result.getSpecCode())) {
			sql.append(" and cd.csd_spec_code='"+specCode+"'   \n ");
		}
		//***医院编码***
		if(StringUtils.hasLength(hospCode)){
			sql.append(" and instr('"+hospCode+"',a.p00010001)>0 and  1=1  ");
		}
		sql.append(" order by e.as_weight  \n ");
		Map<String,Object> condition = new HashMap<>() ;
		List<String> weightList = jdbcTempSeverice.queryForList(sql.toString(), condition , String.class) ;
		return weightList;
	}
	
	@Override
	public Map<String, Object> getQuotasByWeightInterval(IndexCal result, Double weight1, Double weight2) {
		String dateFrom = result.getDateFrom();
		String dateTo = result.getDateTo();
		String stdId = result.getStdId();
		String verRegion = result.getVerRegion();
		String specCode = result.getSpecCode();
		String verCode = result.getVerCode();
		//院区编码
		String hospCode=result.getHospCode(); 
		StringBuilder sql = new StringBuilder();
		sql.append(" select count(distinct ipinfo.P00300001), count(distinct b.pardrg_code), count(ipinfo.pmainindex), sum(e.as_weight)/count(ipinfo.pmainindex), sum(e.as_weight) from drgs.mr_baseinfo a \n ");
		sql.append(" left join drgs.mr_ipinfo ipinfo on a.pmainindex = ipinfo.pmainindex \n ");
		sql.append(" left join drgs.t_patientdrg b   \n ");
		sql.append(" on ipinfo.pmainindex=b.parid and b.ver_region='"+verRegion+"'  \n ");
		sql.append(" left join drgs.t_clispecdrgs cd \n ");
		sql.append(" on cd.csd_drg_code = b.pardrg_code and cd.csd_ver_code = b.ver_code \n ");
		sql.append(" left join drgs.t_annualstd e   \n ");
		sql.append(" on b.pardrg_code=e.as_drg_code and e.AS_ID='"+stdId+"'   \n ");
		sql.append(" where to_char(ipinfo.P00290000,'yyyy-mm-dd') between  '"+dateFrom+"'  and  '"+dateTo+"' \n ");
		sql.append(" and e.as_weight is not null  \n ");
		sql.append(" and ipinfo.P00300001 is not null \n ");
		sql.append(" and b.pardrg_code is not null  \n ");
		sql.append(" and cd.csd_spec_code='"+specCode+"'   \n ");
		if(weight1!=null) {
			sql.append(" and e.as_weight >"+weight1+" \n ");
		}
		if(weight2!=null) {
			sql.append(" and e.as_weight <= "+weight2+" \n ");
		}
		//***医院编码***
		if(StringUtils.hasLength(hospCode)){
			sql.append(" and instr('"+hospCode+"',a.p00010001)>0 \n ");
		}
		Object[] obj = getRepository().createSqlQuery(sql.toString()).singleResult();
		Map<String, Object> resultMap  = new HashMap<>();
		resultMap.put("depsCount", obj[0]!=null?obj[0]:0);
		resultMap.put("drgsCount", obj[1]!=null?obj[1]:0);
		resultMap.put("totalCaseCnt", obj[2]!=null?obj[2]:0);
		resultMap.put("cmi", obj[3]!=null?String.format("%.3f", obj[3]):0);
		resultMap.put("totalWeight", obj[4]!=null?String.format("%.2f", obj[4]):0);
		
		sql = new StringBuilder();
		sql.append(" select count(cd.csd_drg_code) from drgs.t_clispecdrgs cd \n ");
		sql.append(" left join drgs.t_annualstd c \n ");
		sql.append(" on cd.csd_drg_code = c.as_drg_code and c.AS_ID='"+stdId+"' \n ");
		sql.append(" where  1=1  \n ");
		sql.append(" and cd.csd_spec_code='"+specCode+"' \n ");
		sql.append(" and c.as_weight is not null \n ");
		if(weight1!=null)
			sql.append(" and c.as_weight >"+weight1+" \n ");
		if(weight2!=null)
			sql.append(" and c.as_weight <="+weight2+" \n ");
		sql.append(" and cd.csd_ver_code  =  '"+verCode+"' \n ");
		BigDecimal o = getRepository().createSqlQuery(sql.toString()).singleResult();
		resultMap.put("drgsCounts", o);
		
		sql = new StringBuilder();
		sql.append(" select sum(avgDays/as_avg_days*totalcasecnt)/sum(totalcasecnt), sum(avgfee/as_avg_fee*totalcasecnt)/sum(totalcasecnt) from( \n ");
		sql.append(" select b.pardrg_code, c.as_avg_days, c.as_avg_fee, avg(ipinfo.P00320000) avgdays, avg(ipinfo.P00800000) avgfee, count(1) totalCaseCnt  from drgs.mr_baseinfo a \n ");
		sql.append(" left join drgs.mr_ipinfo ipinfo on a.pmainindex = ipinfo.pmainindex \n ");
		sql.append(" left join drgs.t_patientdrg b \n ");
		sql.append(" on ipinfo.pmainindex=b.parid and b.ver_region='"+verRegion+"' \n ");
		sql.append(" left join drgs.t_annualstd c \n ");
		sql.append(" on b.pardrg_code=c.as_drg_code and c.AS_ID='"+stdId+"' \n ");
		sql.append(" left join drgs.t_clispecdrgs cd \n ");
		sql.append(" on cd.csd_drg_code = b.pardrg_code and cd.csd_ver_code = b.ver_code \n ");
		sql.append(" where to_char(ipinfo.P00290000,'yyyy-mm-dd') between  '"+dateFrom+"'  and  '"+dateTo+"' \n ");
		sql.append(" and c.as_drg_code is not null \n ");
		sql.append(" and b.pardrg_code is not null  \n ");
		sql.append(" and cd.csd_spec_code='"+specCode+"'   \n ");
		if(weight1!=null) {
			sql.append(" and c.as_weight >"+weight1+" \n ");
		}
		if(weight2!=null) {
			sql.append(" and c.as_weight <="+weight2+" \n ");
		}
		//***医院编码***
		if(StringUtils.hasLength(hospCode)){
			sql.append(" and instr('"+hospCode+"',a.p00010001)>0 \n ");
		}
		sql.append(" group by b.pardrg_code,c.as_avg_days,c.as_avg_fee \n ");
		sql.append(" )  \n ");
		obj = getRepository().createSqlQuery(sql.toString()).singleResult();
		resultMap.put("timeEff", obj[0]!=null?String.format("%.2f", obj[0]):0);
		resultMap.put("feeEff", obj[1]!=null?String.format("%.2f", obj[1]):0);
		
		sql = new StringBuilder();
		sql.append(" select  \n ");
		sql.append(" case when sum(case when c.as_risk_level=1 then 1 else 0 end)<>0 then \n ");
		sql.append(" sum(case when c.as_risk_level=1 and ipinfo.P00620001=5 then 1 else 0 end)/sum(case when c.as_risk_level=1 then 1 else 0 end)*100 else 0 end risk1DeadRatio, \n ");
		sql.append(" case when sum(case when c.as_risk_level=2 then 1 else 0 end)<>0 then \n ");
		sql.append(" sum(case when c.as_risk_level=2 and ipinfo.P00620001=5 then 1 else 0 end)/sum(case when c.as_risk_level=2 then 1 else 0 end)*100 else 0 end risk2DeadRatio \n ");
		sql.append(" from drgs.mr_baseinfo a \n ");
		sql.append(" left join drgs.mr_ipinfo ipinfo on a.pmainindex = ipinfo.pmainindex \n ");
		sql.append(" left join drgs.t_patientdrg b \n ");
		sql.append(" on ipinfo.pmainindex=b.parid and b.ver_region='"+verRegion+"' \n ");
		sql.append(" left join drgs.t_annualstd c \n ");
		sql.append(" on b.pardrg_code=c.as_drg_code and c.AS_ID='"+stdId+"' \n ");
		sql.append(" left join drgs.t_clispecdrgs cd \n ");
		sql.append(" on cd.csd_drg_code = b.pardrg_code and cd.csd_ver_code = b.ver_code \n ");
		sql.append(" where to_char(ipinfo.P00290000,'yyyy-mm-dd') between  '"+dateFrom+"'  and  '"+dateTo+"' \n ");
		sql.append(" and cd.csd_drg_code is not null \n ");
		sql.append(" and c.as_drg_code is not null \n ");
		sql.append(" and cd.csd_spec_code='"+specCode+"'   \n ");
		if(weight1!=null) {
			sql.append(" and c.as_weight >"+weight1+" \n ");
		}
		if(weight2!=null) {
			sql.append(" and c.as_weight <="+weight2+" \n ");
		}
		//***医院编码***
		if(StringUtils.hasLength(hospCode)){
			sql.append(" and instr('"+hospCode+"',a.p00010001)>0 \n ");
		}
		obj = getRepository().createSqlQuery(sql.toString()).singleResult();
		resultMap.put("risk1DeadRatio", obj[0]!=null?String.format("%.2f", obj[0]):0);
		resultMap.put("risk2DeadRatio", obj[1]!=null?String.format("%.2f", obj[1]):0);
		
		//计算死亡指数
		sql = new StringBuilder();
		sql.append(" select  \n ");
		sql.append(" avg(case when b.deadRatio<>0 then a.riskDeadRatio/b.deadRatio else 0 end) riskDeadScore from ( \n ");
		sql.append(" select \n ");
		sql.append(" b.pardrg_code,    \n ");
		sql.append(" case when count(ipinfo.pmainindex)<>0 then \n ");
		sql.append(" sum(case when ipinfo.P00620001=5 then 1 else 0 end)/count(ipinfo.pmainindex) else 0 end riskDeadRatio \n ");
		sql.append(" from drgs.mr_baseinfo a \n ");
		sql.append(" left join drgs.mr_ipinfo ipinfo on a.pmainindex = ipinfo.pmainindex \n ");
		sql.append(" left join drgs.t_patientdrg b  \n ");
		sql.append(" on ipinfo.pmainindex=b.parid and b.ver_region='"+verRegion+"'  \n ");
		sql.append(" left join drgs.t_annualstd c  \n ");
		sql.append(" on b.pardrg_code=c.as_drg_code and c.AS_ID='"+stdId+"'  \n ");
		sql.append(" left join drgs.t_clispecdrgs cd \n ");
		sql.append(" on cd.csd_drg_code = b.pardrg_code and cd.csd_ver_code = b.ver_code \n ");
		sql.append(" where to_char(ipinfo.P00290000,'yyyy-mm-dd') between  '"+dateFrom+"'  and  '"+dateTo+"' \n ");
		sql.append(" and cd.csd_drg_code is not null  \n ");
		sql.append(" and c.as_drg_code is not null  \n ");
		sql.append(" and cd.csd_spec_code='"+specCode+"'  \n ");
		if(weight1!=null) {
			sql.append(" and c.as_weight >"+weight1+" \n ");
		}
		if(weight2!=null) {
			sql.append(" and c.as_weight <="+weight2+" \n ");
		}
		//***医院编码***
		if(StringUtils.hasLength(hospCode)){
			sql.append(" and instr('"+hospCode+"',a.p00010001)>0 \n ");
		}
		sql.append(" group by b.pardrg_code \n ");
		sql.append(" ) a left join ( \n ");
		sql.append(" select a.as_drg_code,  \n ");
		sql.append(" case when as_total_case<>0 then as_dead_cnt/as_total_case else 0 end deadRatio from drgs.t_annualstd a \n ");
		sql.append(" where a.AS_ID='"+stdId+"' \n ");
		sql.append(" ) b on a.pardrg_code=b.as_drg_code \n ");
		BigDecimal obj1 = getRepository().createSqlQuery(sql.toString()).singleResult();
		resultMap.put("risk0DeadEff", obj1!=null?String.format("%.2f", obj1):0);
	
		return resultMap;
	}
	
	@Cacheable(value = "drgscom3.1.1", key = "'specialGetQuotasByWeightInterval' + #result.dateFrom + #result.dateTo + "
			+ "#result.stdId + #result.verRegion + #result.specCode  + #result.hospCode + #weight1 + #weight2")
	@Override
	public List<Map<String, Object>> getQuotasDepByWeightInter(IndexCal result, String weight1, String weight2) {
		String dateFrom = result.getDateFrom();
		String dateTo = result.getDateTo();
		String stdId = result.getStdId();
		String verRegion = result.getVerRegion();
		String specCode = result.getSpecCode();
		String verCode = result.getVerCode();
		
		//院区编码
		String hospCode=result.getHospCode(); 
		StringBuilder sql = new StringBuilder();
		sql.append(" select dep_code, dep_name, count(distinct b.pardrg_code), count(ipinfo.pmainindex), sum(e.as_weight)/count(ipinfo.pmainindex), sum(e.as_weight) from drgs.mr_baseinfo a \n ");
		sql.append(" left join drgs.mr_ipinfo ipinfo on a.pmainindex = ipinfo.pmainindex \n ");
		sql.append(" left join drgs.t_department dep \n ");
		sql.append(" on ipinfo.P00300001=dep.dep_code \n ");
		sql.append(" left join drgs.t_patientdrg b \n ");
		sql.append(" on ipinfo.pmainindex=b.parid and b.ver_region='"+verRegion+"'    \n ");
		sql.append(" left join drgs.t_clispecdrgs cd \n ");
		sql.append(" on cd.csd_drg_code = b.pardrg_code and cd.csd_ver_code = b.ver_code \n ");
		sql.append(" left join drgs.t_annualstd e    \n ");
		sql.append(" on b.pardrg_code=e.as_drg_code and e.AS_ID='"+stdId+"'    \n ");
		sql.append(" where to_char(ipinfo.P00290000,'yyyy-mm-dd') between  '"+dateFrom+"'  and  '"+dateTo+"' \n ");
		sql.append(" and e.as_weight is not null   \n ");
		sql.append(" and ipinfo.P00300001 is not null  \n ");
		sql.append(" and cd.csd_drg_code is not null   \n ");
		sql.append(" and cd.csd_spec_code='"+specCode+"'   \n ");
		if(weight1!=null) {
			sql.append(" and e.as_weight > "+weight1+" \n ");
		}
		if(weight2!=null) {
			sql.append(" and e.as_weight <= "+weight2+" \n ");
		}
		//***医院编码***
		if(StringUtils.hasLength(hospCode)){
			sql.append(" and instr('"+hospCode+"',a.p00010001)>0 \n ");
		}	
		sql.append(" group by dep_code, dep_name \n ");
		sql.append(" order by dep_code \n ");	
		List<Object[]> list = getRepository().createSqlQuery(sql.toString()).list();
		List<Map<String, Object>> resultList = new LinkedList<>();
		//理论DRGs组数，按专业取
		sql = new StringBuilder();
		sql.append(" select count(cd.csd_drg_code) from drgs.t_clispecdrgs cd \n ");
		if(StringUtils.hasLength(weight1)||StringUtils.hasLength(weight2)){
			sql.append(" left join drgs.t_annualstd c \n ");
			sql.append(" on cd.csd_drg_code=c.as_drg_code and c.AS_ID='"+stdId+"' \n ");
		}
		sql.append(" where  1=1  \n ");
		sql.append(" and cd.csd_spec_code='"+specCode+"' \n ");
		if(StringUtils.hasLength(weight1)||StringUtils.hasLength(weight2)){
			sql.append(" and c.as_weight is not null \n ");
		}
		sql.append(" and cd.csd_ver_code  =  '"+verCode+"' \n ");
		if(StringUtils.hasLength(weight1)) {
			sql.append(" and c.as_weight > "+weight1+" \n ");
		}
		if(StringUtils.hasLength(weight2)) {
			sql.append(" and c.as_weight <= "+weight2+" \n ");
		}
		BigDecimal o = getRepository().createSqlQuery(sql.toString()).singleResult();
		sql = new StringBuilder();
		sql.append(" select count(ipinfo.pmainindex), sum(e.as_weight) from drgs.mr_baseinfo a \n ");
		sql.append(" left join drgs.mr_ipinfo ipinfo on a.pmainindex = ipinfo.pmainindex \n ");
		sql.append(" left join drgs.t_patientdrg b   \n ");
		sql.append(" on ipinfo.pmainindex=b.parid and b.ver_region='"+verRegion+"'   \n ");
		sql.append(" left join drgs.t_clispecdrgs cd \n ");
		sql.append(" on cd.csd_drg_code = b.pardrg_code and cd.csd_ver_code = b.ver_code \n ");
		sql.append(" left join drgs.t_annualstd e   \n ");
		sql.append(" on b.pardrg_code=e.as_drg_code and e.AS_ID='"+stdId+"'   \n ");
		sql.append(" where to_char(ipinfo.P00290000,'yyyy-mm-dd') between  '"+dateFrom+"'  and  '"+dateTo+"' \n ");
		sql.append(" and e.as_weight is not null  \n ");
		sql.append(" and ipinfo.P00300001 is not null \n ");
		sql.append(" and b.pardrg_code is not null  \n ");
		sql.append(" and cd.csd_spec_code='"+specCode+"'   \n ");
		//***医院编码***
		if(StringUtils.hasLength(hospCode)){
			sql.append(" and instr('"+hospCode+"',a.p00010001)>0 \n ");
		}
		Object[] obj = getRepository().createSqlQuery(sql.toString()).singleResult();
		Double totalCaseCnts = obj[0]!=null?Double.parseDouble(obj[0].toString()):0;
		Double totalWeights = obj[1]!=null?Double.parseDouble(obj[1].toString()):0;
		for(Object[] objs: list) {
			Map<String, Object> map = new HashMap<>();
			map.put("ccode", objs[0]);
			map.put("ddesc", objs[1]);
			map.put("drgsCount", objs[2]);
			map.put("totalCaseCnt", objs[3]);
			map.put("cmi", String.format("%.2f", objs[4]));
			map.put("totalWeight", objs[5]);
			map.put("drgsCounts", o);
			Double drgsCount = objs[2]!=null?Double.parseDouble(objs[2].toString()):0;
			Double drgsCounts = o!=null?o.doubleValue():0;
			String drgsCountRatio = String.format("%.2f", drgsCounts!=0?drgsCount/drgsCounts*100:0);
			map.put("drgsCountRatio", drgsCountRatio);
			Double totalCaseCnt = objs[3]!=null?Double.parseDouble(objs[3].toString()):0;
			String totalCaseCntRatio = String.format("%.2f", totalCaseCnts!=0?totalCaseCnt/totalCaseCnts*100:0);
			map.put("totalCaseCntRatio", totalCaseCntRatio);
			Double totalWeight = objs[5]!=null?Double.parseDouble(objs[5].toString()):0;
			String totalWeightRatio = String.format("%.2f", totalWeights!=0?totalWeight/totalWeights*100:0);
			map.put("totalWeightRatio", totalWeightRatio);
			resultList.add(map);
		}
		
		sql = new StringBuilder();
		sql.append(" select dep_code, dep_name, sum(avgDays/as_avg_days*totalcasecnt)/sum(totalcasecnt), sum(avgfee/as_avg_fee*totalcasecnt)/sum(totalcasecnt) from( \n ");
		sql.append(" select dep_code, dep_name, cd.csd_drg_code, c.as_avg_days, c.as_avg_fee, avg(ipinfo.P00320000) avgdays, avg(ipinfo.P00800000) avgfee, count(1) totalCaseCnt from drgs.mr_baseinfo a \n ");
		sql.append(" left join drgs.mr_ipinfo ipinfo on a.pmainindex = ipinfo.pmainindex \n ");
		sql.append(" left join drgs.t_department dep \n ");
		sql.append(" on ipinfo.P00300001=dep.dep_code \n ");
		sql.append(" left join drgs.t_patientdrg b \n ");
		sql.append(" on ipinfo.pmainindex=b.parid and b.ver_region='"+verRegion+"' \n ");
		sql.append(" left join drgs.t_annualstd c \n ");
		sql.append(" on b.pardrg_code=c.as_drg_code and c.AS_ID='"+stdId+"' \n ");
		sql.append(" left join drgs.t_clispecdrgs cd \n ");
		sql.append(" on cd.csd_drg_code = b.pardrg_code and cd.csd_ver_code = b.ver_code \n ");
		sql.append(" where to_char(ipinfo.P00290000,'yyyy-mm-dd') between  '"+dateFrom+"'  and  '"+dateTo+"' \n ");
		sql.append(" and cd.csd_drg_code is not null \n ");  
		sql.append(" and c.as_drg_code is not null \n ");
		sql.append(" and  cd.csd_spec_code='"+specCode+"' \n ");
		if(weight1!=null) {
			sql.append(" and c.as_weight > "+weight1+" \n ");
		}
		if(weight2!=null) {
			sql.append(" and c.as_weight <= "+weight2+" \n ");
		}
		//***医院编码***
		if(StringUtils.hasLength(hospCode)){
			sql.append(" and instr('"+hospCode+"',a.p00010001)>0 \n ");
		}
		sql.append(" group by dep_code, dep_name, cd.csd_drg_code,c.as_avg_days,c.as_avg_fee \n ");
		sql.append(" )  \n ");
		sql.append(" group by dep_code, dep_name \n ");
		sql.append(" order by dep_code \n ");
		list = getRepository().createSqlQuery(sql.toString()).list();
		List<Map<String, Object>> resultList1 = new LinkedList<>();
		for(Object[] objs: list){
			Map<String, Object> map = new HashMap<>();
			map.put("ccode", objs[0]);
			map.put("ddesc", objs[1]);
			map.put("timeEff", String.format("%.2f", objs[2]));
			map.put("feeEff", String.format("%.2f", objs[3]));
			resultList1.add(map);
		}
		resultList = mapList(resultList, resultList1, "");
		
		sql = new StringBuilder();
		sql.append(" select dep_code, dep_name, \n ");
		sql.append(" case when sum(case when c.as_risk_level=1 then 1 else 0 end)<>0 then \n ");
		sql.append(" sum(case when c.as_risk_level=1 and ipinfo.P00620001=5 then 1 else 0 end)/sum(case when c.as_risk_level=1 then 1 else 0 end)*100 else 0 end risk1DeadRatio, \n ");
		sql.append(" case when sum(case when c.as_risk_level=2 then 1 else 0 end)<>0 then \n ");
		sql.append(" sum(case when c.as_risk_level=2 and ipinfo.P00620001=5 then 1 else 0 end)/sum(case when c.as_risk_level=2 then 1 else 0 end)*100 else 0 end risk2DeadRatio \n ");
		sql.append(" from drgs.mr_baseinfo a \n ");
		sql.append(" left join drgs.mr_ipinfo ipinfo on a.pmainindex = ipinfo.pmainindex \n ");
		sql.append(" left join drgs.t_department dep \n ");
		sql.append(" on ipinfo.P00300001=dep.dep_code \n ");
		sql.append(" left join drgs.t_patientdrg b \n ");
		sql.append(" on ipinfo.pmainindex=b.parid and b.ver_region='"+verRegion+"' \n ");
		sql.append(" left join drgs.t_annualstd c \n ");
		sql.append(" on b.pardrg_code=c.as_drg_code and c.AS_ID='"+stdId+"' \n ");
		sql.append(" left join drgs.t_clispecdrgs cd \n ");
		sql.append(" on cd.csd_drg_code = b.pardrg_code and cd.csd_ver_code = b.ver_code \n ");
		sql.append(" where to_char(ipinfo.P00290000,'yyyy-mm-dd') between  '"+dateFrom+"'  and  '"+dateTo+"' \n ");
		sql.append(" and cd.csd_drg_code is not null \n ");
		sql.append(" and c.as_drg_code is not null \n ");
		sql.append(" and cd.csd_spec_code = '"+specCode+"' \n ");
		if(weight1!=null) {
			sql.append(" and c.as_weight > "+weight1+" \n ");
		}
		if(weight2!=null) {
			sql.append(" and c.as_weight <= "+weight2+" \n ");
		}
		//***医院编码***
		if(StringUtils.hasLength(hospCode)){
			sql.append(" and instr('"+hospCode+"',a.p00010001)>0 \n ");
		}	
		sql.append(" group by dep_code, dep_name \n ");
		sql.append(" order by dep_code \n ");
		list = getRepository().createSqlQuery(sql.toString()).list();
		resultList1 = new LinkedList<>();
		for(Object[] objs: list){
			Map<String, Object> map = new HashMap<>();
			map.put("ccode", objs[0]);
			map.put("ddesc", objs[1]);
			map.put("risk1DeadRatio", String.format("%.2f", objs[2]));
			map.put("risk2DeadRatio",  String.format("%.2f", objs[3]));
			resultList1.add(map);
		}
		resultList = mapList(resultList, resultList1, "");
		
		//计算死亡指数
		sql = new StringBuilder();
		sql.append(" select dep_code, \n ");
		sql.append(" avg(case when b.deadRatio<>0 then a.riskDeadRatio/b.deadRatio else 0 end) riskDeadScore from ( \n ");
		sql.append(" select dep_code, \n ");
		sql.append(" b.pardrg_code,    \n ");
		sql.append(" case when count(ipinfo.pmainindex)<>0 then \n ");
		sql.append(" sum(case when ipinfo.P00620001=5 then 1 else 0 end)/count(ipinfo.pmainindex) else 0 end riskDeadRatio \n ");
		sql.append(" from drgs.mr_baseinfo a \n ");
		sql.append(" left join drgs.mr_ipinfo ipinfo on a.pmainindex = ipinfo.pmainindex \n ");
		sql.append(" left join drgs.t_department dep \n ");
		sql.append(" on ipinfo.P00300001=dep.dep_code \n ");
		sql.append(" left join drgs.t_patientdrg b  \n ");
		sql.append(" on ipinfo.pmainindex=b.parid and b.ver_region='"+verRegion+"'  \n ");
		sql.append(" left join drgs.t_annualstd c  \n ");
		sql.append(" on b.pardrg_code=c.as_drg_code and c.AS_ID='"+stdId+"'  \n ");
		sql.append(" left join drgs.t_clispecdrgs cd \n ");
		sql.append(" on cd.csd_drg_code = b.pardrg_code and cd.csd_ver_code = b.ver_code \n ");
		sql.append(" where to_char(ipinfo.P00290000,'yyyy-mm-dd') between  '"+dateFrom+"'  and  '"+dateTo+"' \n ");
		sql.append(" and cd.csd_drg_code is not null  \n ");
		sql.append(" and c.as_drg_code is not null  \n ");
		sql.append(" and cd.csd_spec_code = '"+specCode+"'  \n ");
		if(weight1!=null) {
			sql.append(" and c.as_weight >"+weight1+" \n ");
		}
			
		if(weight2!=null) {
			sql.append(" and c.as_weight <="+weight2+" \n ");
		}
		//***医院编码***
		if(StringUtils.hasLength(hospCode)){
			sql.append(" and instr('"+hospCode+"',a.p00010001)>0 \n ");
		}	
		sql.append(" group by dep_code, b.pardrg_code \n ");
		sql.append(" ) a left join ( \n ");
		sql.append(" select a.as_drg_code,  \n ");
		sql.append(" case when as_total_case<>0 then as_dead_cnt/as_total_case else 0 end deadRatio from drgs.t_annualstd a \n ");
		sql.append(" where a.AS_ID='"+stdId+"' \n ");
		sql.append(" ) b on a.pardrg_code=b.as_drg_code \n ");
		sql.append(" group by dep_code \n ");
		sql.append(" order by dep_code \n ");
		list = getRepository().createSqlQuery(sql.toString()).list();
		resultList1 = new LinkedList<>();
		for(Object[] objs: list){
			Map<String, Object> map = new HashMap<>();
			map.put("ccode", objs[0]);
			map.put("deadEff", String.format("%.2f",objs[1]));
			resultList1.add(map);
		}
		resultList = mapList(resultList, resultList1, "");
		return resultList;
	}
	
	/*@Cacheable(value = "drgscom3.1.1", key = "'specialGetDepsInnerTimeFeeDeadEff' + #result.dateFrom + #result.dateTo + "
			+ "#result.stdYear + #result.specCode + #result.hospCode + #weight1 + #weight2")*/
	@Override
	public List<Map<String, Object>> getDepsInnerTimeFeeDeadEff(IndexCal result, String weight1, String weight2){
		String dateFrom = result.getDateFrom();
		String dateTo = result.getDateTo();
		String specCode = result.getSpecCode();
		String stdId = result.getStdId();
		String verRegion = result.getVerRegion();
		StringBuilder sql = new StringBuilder();
		//院区编码
		String hospCode=result.getHospCode(); 
		sql.append(" select dep_code, dep_name, sum(avgDays/as_avg_days*totalcasecnt)/sum(totalcasecnt), sum(avgfee/as_avg_fee*totalcasecnt)/sum(totalcasecnt) from (  \n ");
		sql.append(" select dep_code, dep_name, cd.csd_drg_code, c.as_avg_days, c.as_avg_fee, avg(ipinfo.P00320000) avgdays, avg(ipinfo.P00800000) avgfee, count(1) totalCaseCnt from drgs.mr_baseinfo a \n ");
		sql.append(" left join drgs.mr_ipinfo ipinfo on a.pmainindex = ipinfo.pmainindex \n ");
		sql.append(" left join drgs.t_department dep  \n ");
		sql.append(" on ipinfo.P00300001=dep.dep_code  \n ");
		sql.append(" left join drgs.t_patientdrg b  \n ");
		sql.append(" on ipinfo.pmainindex=b.parid and b.ver_region='"+verRegion+"'  \n ");
		sql.append(" left join ( \n ");
		sql.append(" select b.pardrg_code as_drg_code, avg(ipinfo.P00320000) as_avg_days, avg(ipinfo.P00800000) as_avg_fee from drgs.mr_baseinfo a \n ");
		sql.append(" left join drgs.mr_ipinfo ipinfo on a.pmainindex = ipinfo.pmainindex \n ");
		sql.append(" left join drgs.t_patientdrg b \n ");
		sql.append(" on ipinfo.pmainindex=b.parid and b.ver_region='"+verRegion+"' \n ");
		sql.append(" where to_char(ipinfo.P00290000,'yyyy-mm-dd') between  '"+dateFrom+"'  and  '"+dateTo+"' \n ");
		//***医院编码***
		if(StringUtils.hasLength(hospCode)){
			sql.append(" and instr('"+hospCode+"',a.p00010001)>0 \n ");
		}
		sql.append(" and b.pardrg_code is not null \n ");
		sql.append(" group by b.pardrg_code \n ");
		sql.append(" ) c on b.pardrg_code=c.as_drg_code \n ");
		sql.append(" left join drgs.t_annualstd s  \n ");
		sql.append(" on s.as_drg_code=b.pardrg_code and s.AS_ID='"+stdId+"'  \n ");
		sql.append(" left join drgs.t_clispecdrgs cd \n ");
		sql.append(" on cd.csd_drg_code = b.pardrg_code and cd.csd_ver_code = b.ver_code \n ");
		sql.append(" where to_char(ipinfo.P00290000,'yyyy-mm-dd') between  '"+dateFrom+"'  and  '"+dateTo+"' \n ");
		sql.append(" and cd.csd_drg_code is not null  \n ");
		sql.append(" and c.as_drg_code is not null  \n ");
		sql.append(" and s.as_weight is not null  \n ");
		sql.append(" and cd.csd_spec_code = '"+specCode+"'  \n ");
		if(weight1!=null) {
			sql.append(" and s.as_weight > "+weight1+" \n ");
		}
		if(weight2!=null) {
			sql.append(" and s.as_weight <= "+weight2+" \n ");
		}
		//***医院编码***
		if(StringUtils.hasLength(hospCode)){
			sql.append(" and instr('"+hospCode+"',a.p00010001)>0 \n ");
		}
		sql.append(" group by dep_code, dep_name, cd.csd_drg_code,c.as_avg_days,c.as_avg_fee  \n ");
		sql.append(" )   \n ");
		sql.append(" group by dep_code, dep_name  \n ");
		sql.append(" order by dep_code  \n ");
		List<Object[]> list = getRepository().createSqlQuery(sql.toString()).list();
		List<Map<String, Object>> resultList = new LinkedList<>();
		for(Object[] objs: list){
			Map<String, Object> map = new HashMap<>();
			map.put("ccode", objs[0]);
			map.put("ddesc", objs[1]);
			map.put("timeEffIn", String.format("%.2f", objs[2]));
			map.put("feeEffIn", String.format("%.2f", objs[3]));
			resultList.add(map);
		}
		//获取院内死亡指数
		sql = new StringBuilder();
		sql.append(" select dep_code,  \n ");
		sql.append(" avg(case when b.deadRatio<>0 then a.riskDeadRatio/b.deadRatio else 0 end) deadEff from (  \n ");
		sql.append(" select dep_code,  \n ");
		sql.append(" b.pardrg_code,     \n ");
		sql.append(" case when count(ipinfo.pmainindex)<>0 then  \n ");
		sql.append(" sum(case when ipinfo.P00620001=5 then 1 else 0 end)/count(ipinfo.pmainindex) else 0 end riskDeadRatio  \n ");
		sql.append(" from drgs.mr_baseinfo a \n ");
		sql.append(" left join drgs.mr_ipinfo ipinfo on a.pmainindex = ipinfo.pmainindex \n ");
		sql.append(" left join drgs.t_department dep  \n ");
		sql.append(" on ipinfo.P00300001=dep.dep_code  \n ");
		sql.append(" left join drgs.t_patientdrg b   \n ");
		sql.append(" on ipinfo.pmainindex=b.parid and b.ver_region='"+verRegion+"'   \n ");
		sql.append(" left join drgs.t_annualstd c   \n ");
		sql.append(" on b.pardrg_code=c.as_drg_code and c.AS_ID='"+stdId+"'   \n ");
		sql.append(" left join drgs.t_clispecdrgs cd \n ");
		sql.append(" on cd.csd_drg_code = b.pardrg_code and cd.csd_ver_code = b.ver_code \n ");
		sql.append(" where to_char(ipinfo.P00290000,'yyyy-mm-dd') between  '"+dateFrom+"'  and  '"+dateTo+"'   \n ");
		sql.append(" and cd.csd_drg_code is not null   \n ");
		sql.append(" and c.as_drg_code is not null   \n ");
		sql.append(" and cd.csd_spec_code = '"+specCode+"'   \n ");
		if(weight1!=null) {
			sql.append(" and s.as_weight > "+weight1+" \n ");
		}
		if(weight2!=null) {
			sql.append(" and s.as_weight <= "+weight2+" \n ");
		}
		//***医院编码***
		if(StringUtils.hasLength(hospCode)){
			sql.append(" and instr('"+hospCode+"',a.p00010001)>0 \n ");
		}
		sql.append(" group by dep_code, b.pardrg_code  \n ");
		sql.append(" ) a left join (  \n ");
		sql.append(" select b.pardrg_code as_drg_code, case when count(1)<>0 then \n ");
		sql.append(" sum(case when ipinfo.P00620001=5 then 1 else 0 end)/count(1) else 0 end deadRatio \n ");
		sql.append(" from drgs.mr_baseinfo a \n ");
		sql.append(" left join drgs.mr_ipinfo ipinfo on a.pmainindex = ipinfo.pmainindex \n ");
		sql.append(" left join drgs.t_patientdrg b \n ");
		sql.append(" on ipinfo.pmainindex=b.parid and b.ver_region='"+verRegion+"' \n ");
		sql.append(" where to_char(ipinfo.P00290000,'yyyy-mm-dd') between  '"+dateFrom+"'  and  '"+dateTo+"'   \n ");
		//***医院编码***
		if(StringUtils.hasLength(hospCode)){
			sql.append(" and instr('"+hospCode+"',a.p00010001)>0 \n ");
		}
		sql.append(" group by b.pardrg_code \n ");
		sql.append(" ) b on a.pardrg_code=b.as_drg_code  \n ");
		sql.append(" group by dep_code  \n ");
		sql.append(" order by dep_code  \n ");
		list = getRepository().createSqlQuery(sql.toString()).list();
		List<Map<String, Object>> resultList1 = new LinkedList<>();
		for(Object[] objs: list){
			Map<String, Object> map = new HashMap<>();
			map.put("ccode", objs[0]);
			map.put("deadEffIn", String.format("%.2f", objs[1]));
			resultList1.add(map);
		}
		resultList = mapList(resultList, resultList1, "");
		return resultList;
	}
	
	/*@Cacheable(value = "drgscom3.1.1", key = "'specialGetDRGsByDepsOrWeight' + #result.dateFrom + #result.dateTo + "
			+ "#result.stdYear + #result.specCode + #result.hospCode + #weight1 + #weight2")*/
	@Override
	public List<Map<String, Object>> getDRGsByDepsOrWeight(IndexCal result, String weight1, String weight2){
		String depCode = result.getPardepCode();
		String dateFrom = result.getDateFrom();
		String dateTo = result.getDateTo();
		String specCode = result.getSpecCode();
		String stdId = result.getStdId();
		String verRegion = result.getVerRegion();
		//院区编码
		String hospCode=result.getHospCode(); 
		StringBuilder sql = new StringBuilder();
		sql.append(" select drg_code ccode, drg_desc ddesc, as_weight asWeight, as_risk_level asRiskLevel, as_avg_days asAvgDays, as_avg_fee asAvgFee,  \n ");
		sql.append(" case when as_total_case<>0 then as_dead_cnt/as_total_case*100 else 0 end asDeadRatio, \n ");
		sql.append(" avg(ipinfo.P00320000) avgDays, \n ");
		sql.append(" avg(ipinfo.P00800000) avgFee, \n ");
		sql.append(" count(ipinfo.pmainindex) totalCaseCnt,  \n ");
		sql.append(" sum(e.as_weight) totalWeight,  \n ");
		sql.append(" avg(ipinfo.P00320000)/e.as_avg_days timeEff,  \n ");
		sql.append(" avg(ipinfo.P00800000)/e.as_avg_fee feeEff, \n ");
		sql.append(" case when sum(case when e.as_weight is not null then 1 else 0 end)<>0 then \n ");
		sql.append(" sum(as_weight)/sum(case when e.as_weight is not null then 1 else 0 end) else 0 end cmi , \n ");
		sql.append(" case when count(ipinfo.pmainindex)<>0 then sum(case when ipinfo.P00620001=5 then 1 else 0 end)/count(ipinfo.pmainindex)*100 else 0 end deadRatio, \n ");
		sql.append(" case when (case when as_total_case<>0 then as_dead_cnt/as_total_case*100 else 0 end)<>0 then (case when count(ipinfo.pmainindex)<>0 then sum(case when ipinfo.P00620001=5 then 1 else 0 end)/count(ipinfo.pmainindex) else 0 end)/(case when as_total_case<>0 then as_dead_cnt/as_total_case else 0 end) else 0 end deadEff \n ");
		sql.append(" from drgs.mr_baseinfo a \n ");
		sql.append(" left join drgs.mr_ipinfo ipinfo on a.pmainindex = ipinfo.pmainindex \n ");
		sql.append(" left join drgs.t_department dep  \n ");
		sql.append(" on ipinfo.P00300001=dep.dep_code  \n ");
		sql.append(" left join drgs.t_patientdrg b     \n ");
		sql.append(" on ipinfo.pmainindex=b.parid and b.ver_region='"+verRegion+"'   \n ");
		sql.append(" left join drgs.t_clispecdrgs cd \n ");
		sql.append(" on cd.csd_drg_code = b.pardrg_code and cd.csd_ver_code = b.ver_code \n ");
		sql.append(" left join drgs.t_drgs drg \n ");
		sql.append(" on cd.csd_drg_code = drg.drg_code and cd.csd_ver_code = drg.ver_code \n ");
		sql.append(" left join drgs.t_annualstd e     \n ");
		sql.append(" on b.pardrg_code=e.as_drg_code and e.AS_ID='"+stdId+"'     \n ");
		sql.append(" where to_char(ipinfo.P00290000,'yyyy-mm-dd') between  '"+dateFrom+"'  and  '"+dateTo+"'   \n ");
//		sql.append(" and e.as_weight is not null    \n ");
		sql.append(" and ipinfo.P00300001 is not null   \n ");
		sql.append(" and b.pardrg_code is not null  \n ");
		/*sql.append(" and c.csd_drg_desc is not null \n ");*/
		sql.append(" and cd.csd_spec_code = '"+specCode+"'    \n ");
		if(StringUtils.hasLength(depCode)) {
			sql.append(" and dep.dep_code='"+depCode+"' \n ");
		}
		if(weight1!=null) {
			sql.append(" and as_weight >"+weight1+" \n ");
		}
		if(weight2!=null) {
			sql.append(" and as_weight <="+weight2+" \n ");
		}
		//***医院编码***
		if(StringUtils.hasLength(hospCode)){
			sql.append(" and instr('"+hospCode+"',a.p00010001)>0 \n ");
		}
		sql.append(" group by drg_code, drg_desc, as_weight, as_risk_level, as_avg_days, as_avg_fee, as_dead_cnt, as_total_case \n ");
		sql.append(" order by drg_code  \n ");
		Map<String,Object> paramMap = new HashMap<>() ;
		List<Map<String,Object>> list = jdbcTempSeverice.queryForList(sql.toString(), paramMap) ;
		List<Map<String, Object>> resultList = new LinkedList<>();
		
		sql = new StringBuilder();
		sql.append(" select count(ipinfo.pmainindex), sum(e.as_weight) from drgs.mr_ipinfo ipinfo    \n ");
		sql.append(" left join drgs.t_department dep \n ");
		sql.append(" on dep.dep_code=ipinfo.P00300001 \n ");
		sql.append(" left join drgs.t_patientdrg b    \n ");
		sql.append(" on ipinfo.pmainindex=b.parid and b.ver_region='"+verRegion+"'    \n ");
		sql.append(" left join drgs.t_clispecdrgs cd \n ");
		sql.append(" on cd.csd_drg_code = b.pardrg_code and cd.csd_ver_code = b.ver_code \n ");
		sql.append(" left join drgs.t_annualstd e  \n ");
		sql.append(" on b.pardrg_code=e.as_drg_code and e.AS_ID='"+stdId+"' \n ");
		sql.append(" where to_char(ipinfo.P00290000,'yyyy-mm-dd') between  '"+dateFrom+"'  and  '"+dateTo+"'   \n ");
		sql.append(" and e.as_weight is not null   \n ");
		sql.append(" and ipinfo.P00300001 is not null  \n ");
		sql.append(" and b.pardrg_code is not null   \n ");
		sql.append(" and cd.csd_spec_code='"+specCode+"'   \n ");
		if(StringUtils.hasLength(depCode))
				sql.append(" and dep.dep_code='"+depCode+"'  \n ");
		Object[] obj = getRepository().createSqlQuery(sql.toString()).singleResult();
		Double totalCaseCnts = obj[0]!=null?Double.parseDouble(obj[0].toString()):0;
		Double totalWeights = obj[1]!=null?Double.parseDouble(obj[1].toString()):0;
		
		for(Map<String,Object> mapQuo: list){
			Map<String, Object> map = new HashMap<>();
			map.put("ccode", mapQuo.get("CCODE"));
			map.put("ddesc", mapQuo.get("DDESC"));
			map.put("asWeight", mapQuo.get("ASWEIGHT"));
			map.put("asRiskLevel", mapQuo.get("ASRISKLEVEL"));
			map.put("asAvgDays", mapQuo.get("ASAVGDAYS"));
			map.put("asAvgFee", mapQuo.get("ASAVGFEE"));
			map.put("asDeadRatio", mapQuo.get("ASDEADRATIO")!=null?String.format("%.2f", mapQuo.get("ASDEADRATIO")):null);
			map.put("avgDays", mapQuo.get("AVGDAYS")!=null?String.format("%.2f", mapQuo.get("AVGDAYS")):null);
			map.put("avgFee", mapQuo.get("AVGFEE")!=null?String.format("%.2f", mapQuo.get("AVGFEE")):null);
			map.put("totalCaseCnt", mapQuo.get("TOTALCASECNT"));
			map.put("totalWeight", mapQuo.get("TOTALWEIGHT"));
			map.put("timeEff", mapQuo.get("TIMEEFF")!=null?String.format("%.2f", mapQuo.get("TIMEEFF")):null);
			map.put("feeEff", mapQuo.get("FEEEFF")!=null?String.format("%.2f", mapQuo.get("FEEEFF")):null);
			map.put("cmi", mapQuo.get("CMI")!=null?String.format("%.2f", mapQuo.get("CMI")):null);
			map.put("deadRatio", mapQuo.get("DEADRATIO")!=null?String.format("%.2f", mapQuo.get("DEADRATIO")):null);
			map.put("deadEff", mapQuo.get("DEADEFF")!=null?String.format("%.2f", mapQuo.get("DEADEFF")):null);
			double totalCaseCnt = mapQuo.get("TOTALCASECNT")!=null?Double.parseDouble(mapQuo.get("TOTALCASECNT").toString()):0;
			double totalCaseCntRatio = totalCaseCnts!=0?totalCaseCnt/totalCaseCnts*100:0;
			double totalWeight = mapQuo.get("TOTALCASECNT")!=null?Double.parseDouble(mapQuo.get("TOTALCASECNT").toString()):0;
			double totalWeightRatio = totalWeights!=0?totalWeight/totalWeights*100:0;
			map.put("totalCaseCnts", totalCaseCnts);
			map.put("totalWeights", totalWeights);
			map.put("totalCaseCntRatio", String.format("%.2f", totalCaseCntRatio));
			map.put("totalWeightRatio", String.format("%.2f", totalWeightRatio));
			resultList.add(map);
		}
		return resultList;
	}
	
	public static List<Map<String, Object>> mapList(List<Map<String, Object>> list1, List<Map<String, Object>> list2, String ss){
    	int i=0;
    	int j=0;
    	int iMax=list1.size();
    	int jMax=list2.size();
    		for(i=0; i<iMax; i++){
        		int k=j;
        		Map<String, Object> map1 = list1.get(i);
        		String ccode1 = map1.get("ccode")!=null?map1.get("ccode").toString():"";
        		while(true){
        			if(k==jMax) break;
        			Map<String, Object> map2 = list2.get(k);
            		String ccode2 = map2.get("ccode")!=null?map2.get("ccode").toString():"";
        			if(ccode2.equals(ccode1)){
        				 for(String dataKey : map2.keySet())   {    
        					 map1.put(dataKey+ss, map2.get(dataKey));
        				 } 
        				 j++;
        				 break;
        			}
        			k++;
        		}
        	}
    	return list1;
    }
	
	@Cacheable(value = "drgscom3.1.1", key = "'specialGetNotInDRGs' + #result.dateFrom + #result.dateTo + "
			+ "#result.stdId + #result.verRegion + #result.specCode + #result.hospCode")
	@Override
	public List<Object[]> getNotInDRGs(IndexCal result) {
		String dateFrom = result.getDateFrom();
		String dateTo = result.getDateTo();
		String stdId = result.getStdId();
		String verRegion = result.getVerRegion();
		String specCode = result.getSpecCode();
		//院区编码
		String hospCode=result.getHospCode(); 
		StringBuilder sql = new StringBuilder();
		sql.append(" select drg.drg_code,drg.drg_desc, adr.adrg_code, adr.adrg_desc, mdc.mdc_code, mdc.mdc_desc, s.as_weight, s.as_avg_days, s.as_avg_fee, s.as_risk_level \n ");
		sql.append(" from drgs.t_clispecdrgs cd \n ");
		sql.append(" left join drgs.t_drgs drg on cd.csd_drg_code = drg.drg_code and cd.csd_ver_code = drg.ver_code \n ");
		sql.append(" left join drgs.t_annualstd s \n ");
		sql.append(" on drg.drg_code=s.as_drg_code and s.AS_ID='"+stdId+"' \n ");
		sql.append(" left join drgs.t_adrg adr \n ");
		sql.append(" on adr.adrg_code=drg.adrg_code and adr.ver_code=drg.ver_code  \n ");
		sql.append(" left join drgs.t_mdcgroup mdc \n ");
		sql.append(" on mdc.mdc_code=adr.mdc_code and mdc.ver_code=adr.ver_code \n ");
		sql.append(" where cd.csd_spec_code ='"+specCode+"' \n ");
		sql.append(" and cd.csd_ver_code  = ( \n ");
		sql.append(" select aa.ver_code from ( \n ");
		sql.append(" select  b.ver_code, count(1) casecntrank  \n ");
		sql.append(" from DRGs.Mr_Baseinfo a  \n ");
		sql.append(" left join drgs.mr_ipinfo ipinfo \n ");
		sql.append(" on a.pmainindex = ipinfo.pmainindex  \n ");
		sql.append(" left join DRGs.t_patientdrg b  \n ");
		sql.append(" on a.pmainindex=b.parid and b.ver_region='"+verRegion+"'  \n ");
		sql.append(" where to_char(ipinfo.p00290000,'yyyy-mm-dd')  between  '"+dateFrom+"'  and  '"+dateTo+"' \n ");
		//***医院编码***
		if(StringUtils.hasLength(hospCode)){
			sql.append(" and instr('"+hospCode+"',a.p00010001)>0 \n ");
		}
		sql.append(" group by b.ver_code order by casecntrank desc \n ");
		sql.append(" )aa where rownum = 1  \n ");
		sql.append(" ) \n ");
		
		sql.append(" and not exists ( \n ");
		sql.append(" select 1 from drgs.t_clispecdrgs csd \n ");
		sql.append(" left join drgs.t_drgs dg \n ");
		sql.append(" on csd.csd_drg_code = dg.drg_code and csd.csd_ver_code = dg.ver_code \n ");
		sql.append(" left join drgs.t_patientdrg c \n ");
		sql.append(" on dg.drg_code=c.pardrg_code and c.ver_region='"+verRegion+"' \n ");
		sql.append(" left join drgs.mr_baseinfo a on a.pmainindex = c.parid \n ");
		sql.append(" left join drgs.mr_ipinfo d \n ");
		sql.append(" on c.parid=d.pmainindex \n ");
		sql.append(" where to_char(d.P00290000,'yyyy-mm-dd') between  '"+dateFrom+"'  and  '"+dateTo+"' \n ");
		sql.append(" and dg.drg_code=drg.drg_code \n ");
		//***医院编码***
		if(StringUtils.hasLength(hospCode)){
			sql.append(" and instr('"+hospCode+"',a.p00010001)>0 \n ");
		}
		sql.append(" ) \n ");
		List<Object[]> resultList = getRepository().createSqlQuery(sql.toString()).list();
		return resultList;
	}

	@Override
	public List<Map<String, Object>> multithreadQuery(final List<Map<String, Object>> query) {
		List<Map<String, Object>> resultList = new ArrayList<>() ;
		ExecutorService threadPool =  Executors.newFixedThreadPool(query.size());  
        CompletionService<Map<String,Object>> completionService = new ExecutorCompletionService<Map<String,Object>>(threadPool);  
        for(final Map<String, Object> map:query){  
            completionService.submit(new Callable<Map<String,Object>>() {  
                @Override  
                public Map<String,Object> call() throws Exception {  
                	String sql = map.get("sql").toString() ;
                	Map<String,Object> resultMap = new HashMap<>() ;
                	Map<String,Object> conditon = (Map<String, Object>) map.get("condition") ;
                	List<Map<String,Object>> resultList = jdbcTempSeverice.queryForList(sql, conditon) ;
                	resultMap.put("queryId", map.get("queryId")) ;
                	resultMap.put("resultList", resultList) ;
                    return resultMap;  
                }  
            });  
        }  
        for(int i=0;i<query.size();i++){  
            try {  
            	query.get(i).get("queryId") ;
            	resultList.add(completionService.take().get()) ;
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            } catch (ExecutionException e) {  
                e.printStackTrace();  
            }  
        }  
        threadPool.shutdown();
		return resultList ;
    }  
}
